#ifndef BARCODESCAN_H
#define BARCODESCAN_H

#include <termios.h>
#include <sys/types.h>
#include <QString>
#include <QThread>
#include <QObject>

#define MAX_BUF_SZ 40
#define BAUD_RATE B57600       // 条码扫描仪的串口通信波特率57600bps
#define COM2 "/dev/ttyUSB0"

class BarcodeScan : public QThread
{
    Q_OBJECT

private:
    QObject* parent;
    int serialFd;
    termios oldSerialTermio;
    termios serialTermio;
    char barcodeBuf[MAX_BUF_SZ];
    QString errorMsg;

    bool ttyInit();
    void ttyFlush();
    void ttyEnd();
    ssize_t ttyRead(char* buf, size_t bufSize);
    int ttyWrite(const char* buf, size_t bufSize);
    int ttyWriteCmd(const char* buf, size_t bufSize);
protected:
    void run();

public:
    explicit BarcodeScan(QObject *parent = 0);
    virtual ~BarcodeScan();
    QString* readBarcode();
    bool init();
    QString getErrorMsg();

signals:
    void barcodeScanned(const QString* barcode);
    void updateStatus(const QString& msg, bool flag);
public slots:
    void quitThread();
};

#endif // BARCODESCAN_H
